import { getToken, setToken } from '@/utils/auth'
import { getInfo, getInfoId, login } from '@/api/user'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    updatetoken(state, token) {
      state.token = token
      setToken(token)
    },
    updataUserinfo(state, userInfo) {
      state.userInfo = userInfo
    },
    reomveUserInfo(state) {
      state.userInfo = {}
    },
    removetoken(state) {
      state.token = ''
    }
  },
  actions: {
    async  userLogin(context, loginForm) {
      const res = await login(loginForm)
      context.commit('updatetoken', res.data)
      return res
    },
    async postinfo(context) {
      const res = await getInfo()
      const res1 = await getInfoId(res.data.userId)
      context.commit('updataUserinfo', { ...res.data, ...res1.data })
      return { ...res.data, ...res1.data }
    },
    logout(context) {
      context.commit('reomveUserInfo')
      context.commit('removetoken')
    }

  },
  getters: {}
}
